package com.threadmethod.volatiled;

/**
 * @PackageName:com.threadmethod.volatiled
 * @ClassName:Test041
 * @Description:
 * @Author: zhangxiaoyu
 * @date 2020/12/10 15:13
 */
public class Test01 {

    public static void main(String[] args){
         //创建对象
        PrintString printString = new PrintString();
        printString.printStringMonth();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("main打印标志");
        printString.setContinuePrint(false);
        //运行程序查看结果 程序不会停止，因为方法调用后一直处于死循环状态，执行不到false语句
        //解决办法：使用多线程

    }


    //定义一个类
    static class PrintString{

        public void setContinuePrint(boolean continuePrint) {
            this.continuePrint = continuePrint;
        }

        //定义一个变量
        private  boolean continuePrint = true;



        public void printStringMonth(){

            while (continuePrint){
                System.out.println(Thread.currentThread().getName());
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
